﻿using PuppeteerSharp;
using Q.Spider.Common;
using Q.Spider.Core;
using Q.Spider.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using Request = Q.Spider.Core.Models.Request;

namespace Q.Spider.Sample.Flow
{
    public class Midjourney : Core.FlowBase
    {


        public override async Task RunAsync(FlowContext Context)
        {
            //没有做自动登录，因为有ip限制
            //所以要先启动一下手动登录，然后重启项目
            await GotoRoomImage();
        }


        public async Task GotoRoomImage()
        {
            

            //进入newbies-153房间
            await Engine.StartLoad("https://discord.com/channels/662267976984297473/1008571152479826060");


            //await Task.Delay(10000000);
            try
            {
                await Engine.WaitForSelectorAsync(".originalLink-Azwuo9", new WaitForSelectorOptions()
                {
                    Timeout = 30000
                });
            }
            catch (Exception)
            {
                LogHelper.Logger.Error("超时：" + Request.Uri);
                Context.State = FlowContextHandelState.错误;
                return;
            }
            var url = await Engine.GetDomAttrAsync(".originalLink-Azwuo9", "href");

            LogHelper.Logger.Info($"url:{url}");

            Context.State = FlowContextHandelState.成功;
            return;
        }








        public static void AddTestRequest(QSpiderCore core)
        {
            var requestCity = new Request("https://discord.com/channels/662267976984297473/1008571152479826060")
            {
                //指定流处理
                FlowTpyes = new List<Type>() { typeof(Midjourney) }
            };
            core.AddRequest(requestCity);

        }
    }
}
